package com.xindaoapp.happypet.activity;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.bean.CanShuInfo;
import com.xindaoapp.happypet.bean.GoodsAttr;
import com.xindaoapp.happypet.bean.GoodsAttr.AttrValue;
import com.xindaoapp.happypet.bean.Product;
import com.xindaoapp.happypet.bean.ProductInfor;
import com.xindaoapp.happypet.bean.ProductInforEntity;
import com.xindaoapp.happypet.utils.ProgressHUD;
import com.xindaoapp.happypet.view.MyGridView;

public class ProductInforActivity extends UMBaseActivity {
    private final String TAG = "ProductInforActivity";
    private Context mContext;
    private TextView title;
    private TextView project;
    private TextView standard;
    private TextView deliery;
    private ImageButton cancle;
    private String goodsID;

    private LinearLayout ll;

    private List<GoodsAttr> goods_attr;

    private Product pro;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_infor);
        goodsID = getIntent().getStringExtra("productID");
        mContext = ProductInforActivity.this;

        pro = (Product) getIntent().getSerializableExtra("pro");

        initView();

        getData();

        // if (pro.goods_attr != null && pro.goods_attr.size() > 0) {
        // goods_attr = pro.goods_attr;
        // initGoodsAttr(goods_attr);
        // }
        getGoodsInfo(goodsID);
    }

    private void getGoodsInfo(String id) {
        RequestParams params = new RequestParams();
        params.put("goodsID", id);
        AddClient.get(Address.GOODSINFO, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                CanShuInfo parse = null;
                try {
                    parse = JSON.parseObject(content, CanShuInfo.class);
                    if (parse.getStatus().equals("1")) {
                        goods_attr = parse.data.goods_attr;
                        initGoodsAttr(goods_attr);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                super.onSuccess(statusCode, content);
            }

            @Override
            public void onFailure(Throwable error, String content) {
                super.onFailure(error, content);
            }
        });
    }

    private void initGoodsAttr(List<GoodsAttr> attr) {
        ll = (LinearLayout) findViewById(R.id.ll);

        // ll = (LinearLayout) findViewById(R.id.ll);
        // MyGridView gv = new MyGridView(mContext);
        // gv.setNumColumns(3);
        // System.out.println(goods_attr2.size() + "goods_attr2");
        // for (int i = 0; i < goods_attr2.size(); i++) {
        // final Adapter1 adapter = new Adapter1(attr.get(page - 1).attr_value);
        // gv.addView(tv);
        // }
        // ll.addView(gv);
        int size = attr.size();
        if (size == 1) {
            inflate(attr, 1);
        }
        if (size == 2) {
            inflate(attr, 1);
            inflate(attr, 2);
        }
        if (size == 3) {
            inflate(attr, 1);
            inflate(attr, 2);
            inflate(attr, 3);
        }
        if (size == 4) {
            inflate(attr, 1);
            inflate(attr, 2);
            inflate(attr, 3);
            inflate(attr, 4);
        }
    }

    private StringBuilder sb;

    private void inflate(List<GoodsAttr> attr, int page) {
        sb = new StringBuilder();
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View inflate = inflater.inflate(R.layout.info_grid, null);
        MyGridView gv = (MyGridView) inflate.findViewById(R.id.shop_brand);
        TextView tv = (TextView) inflate.findViewById(R.id.attr);
        tv.setText(attr.get(page - 1).attr_name + ":");



        TextView tv1 = new TextView(mContext);
        tv1.setTextColor(getResources().getColor(R.color.info_common));
        sb.append(attr.get(page - 1).attr_name + ":");
        for (int i = 0; i < attr.get(page - 1).attr_value.size(); i++) {
            sb.append(attr.get(page - 1).attr_value.get(i).attr_value + "     ");
        }

        SpannableString s = new SpannableString(sb.toString());
        s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.info_right)),
                attr.get(page - 1).attr_name.length() + 1, sb.toString().length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv1.setText(s);
        tv1.setTextSize(16);
        LayoutParams lp2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp2.topMargin = 15;
        lp2.leftMargin = 15;
        tv1.setLayoutParams(lp2);
        ll.addView(tv1);



        // gv.setNumColumns(3);
        // if(page==1){
        // final Adapter1 adapter = new Adapter1(attr.get(page - 1).attr_value);
        // gv.setAdapter(adapter);
        // }
        // if(page==2){
        // final Adapter1 adapter2 = new Adapter1(attr.get(page - 1).attr_value);
        // gv.setAdapter(adapter2);
        // }
        // if(page==3){
        // final Adapter1 adapter3 = new Adapter1(attr.get(page - 1).attr_value);
        // gv.setAdapter(adapter3);
        // }
        // if(page==4){
        // final Adapter1 adapter4 = new Adapter1(attr.get(page - 1).attr_value);
        // gv.setAdapter(adapter4);
        // }
        // LayoutParams lp2 = new LayoutParams(LayoutParams.WRAP_CONTENT,
        // LayoutParams.WRAP_CONTENT);
        // lp2.topMargin = 10;
        // inflate.setLayoutParams(lp2);
        // ll.addView(inflate);
    }

    private class Adapter1 extends BaseAdapter {
        private final List<AttrValue> values;

        public Adapter1(List<AttrValue> values) {
            super();
            this.values = values;
        }

        @Override
        public int getCount() {
            return values.size();
        }

        @Override
        public Object getItem(int position) {
            return values.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(getApplicationContext(), R.layout.info_text, null);
                holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv1.setBackgroundColor(Color.WHITE);
            holder.tv1.setText(values.get(position).attr_value);
            return convertView;
        }
    }

    class ViewHolder {
        TextView tv1;
    }

    private void initView() {
        cancle = (ImageButton) findViewById(R.id.cancel);

        title = (TextView) findViewById(R.id.goods_title);
        project = (TextView) findViewById(R.id.goods_project);
        standard = (TextView) findViewById(R.id.goods_standard);
        deliery = (TextView) findViewById(R.id.goods_deliery);

        cancle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
    }

    private void initData(ProductInfor infor) {
        title.setText(infor.goods_name);
        project.setText("适用对象:" + "成狗");
        standard.setText("规格:" + infor.goods_weight + "KG");
        deliery.setText("商品配送:" + "快递");

        project.setText(changeTextColor(project, 5, 2));
        standard.setText(changeTextColor(standard, 3, infor.goods_weight.length() + 2));
        deliery.setText(changeTextColor(deliery, 5, 2));

    }

    private void getData() {
        RequestParams params = new RequestParams();
        params.put("goodsID", goodsID);

        final ProgressHUD progressHUD = ProgressHUD.show(mContext, "正在加载...", true, true, null);
        AddClient.get(Address.GOODS_INFO, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                if (null != progressHUD && progressHUD.isShowing()) {
                    progressHUD.dismiss();
                }
                ProductInforEntity entity = null;

                try {
                    entity = JSON.parseObject(content, ProductInforEntity.class);
                    if (null == entity) {
                        Toast.makeText(mContext, R.string.get_error_data, 0).show();
                        return;
                    }
                    ProductInfor infor = entity.data;

                    if (null == infor) {
                        Toast.makeText(mContext, R.string.get_error_data, 0).show();
                        return;
                    }

                    initData(infor);
                } catch (Exception e) {
                }
            }

            @Override
            public void onFailure(Throwable error, String content) {
                if (null != progressHUD && progressHUD.isShowing()) {
                    progressHUD.dismiss();
                }
                super.onFailure(error, content);
            }
        });
    }

    private SpannableStringBuilder changeTextColor(TextView tv, int start, int end) {
        SpannableStringBuilder builder = new SpannableStringBuilder(tv.getText().toString());
        ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.parseColor("#4A4A4A"));
        builder.setSpan(redSpan, start, start + end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return builder;
    }
}
